/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package escom.ipn.administrador;

import escom.ipn.modelo.delegate.adminSectorDelegate;
import escom.ipn.modelo.delegate.adminTipoUsuarioDelegate;
import escom.ipn.modelo.delegate.adminUsuarioDelegate;
import escom.ipn.modelo.dto.SectorClass;
import escom.ipn.modelo.dto.UsuarioClass;
import escom.ipn.modelo.dto.tipoUsuarioClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author db2admin
 */
@WebServlet(name = "actualizaUsuario", urlPatterns = {"/actualizaUsuario"})
public class actualizaUsuario extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int catTipo,sector, jornada;
        String nombre,apPaterno,apMaterno, dir, tel, usuario, pass;
        String html;
        
        //Declaracion de clases manejadoras
        adminUsuarioDelegate del = new adminUsuarioDelegate();
        UsuarioClass a = new UsuarioClass();
        adminTipoUsuarioDelegate delU = new adminTipoUsuarioDelegate();
        tipoUsuarioClass u = new tipoUsuarioClass();
        adminSectorDelegate delS = new adminSectorDelegate();
        SectorClass s = new SectorClass();
        List usuarios;
        List sectores;
                
       
        try {
            //asignar nuevo id de Tipo Usuario
            u.setNombre(request.getParameter("tipoUsuario"));
            usuarios=delU.getUsuarioID(u);
            u= (tipoUsuarioClass) usuarios.get(0);         
            
            
            //asignar nuevo id de Sector
        
            s.setNombre(request.getParameter("Sector"));
            sectores=delS.getSectorID(s);
            s= (SectorClass) sectores.get(0); 
        } catch (SQLException ex) {
            Logger.getLogger(actualizaUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        //Pedir datos
        a.setId_usuario(Integer.parseInt(request.getParameter("ID")));
        a.setCAT_TIPO_USUARIO_id_tipo_usuario(u.getId());
        a.setSECTOR_id_sector(s.getId());
        a.setNombre(request.getParameter("nombre"));
        a.setAp_paterno(request.getParameter("ap_paterno"));
        a.setAp_materno(request.getParameter("ap_materno"));
        a.setDireccion(request.getParameter("direccion"));
        a.setTelefono(request.getParameter("tel"));
        a.setUsuario(request.getParameter("usuario"));
        a.setPassword(request.getParameter("password1"));
        a.setJornada(Integer.parseInt(request.getParameter("jornada")));
     
        try {
            if(a!=null)
            del.updateUsuario(a);
            //String redirectURL = "Administrador/administrador.jsp#tabusuario";
            //response.sendRedirect(redirectURL);
        } catch (SQLException ex) {
           out.println( "Fracaso");
        }              
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
